import vue from '@vitejs/plugin-vue';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { defineConfig } from 'vite';

const __dirname = dirname(fileURLToPath(import.meta.url));

// https://vite.dev/config/
export default defineConfig({
  build: {
    lib: {
      // entry keys are mapped to generated filenames in dist directory.
      // For example: index will be index.js & index.cjs.
      entry: {
        index: resolve(__dirname, 'src/index.ts'),
        auth: resolve(__dirname, 'src/auth/index.ts'),
        config: resolve(__dirname, 'src/config/index.ts'),
        constants: resolve(__dirname, 'src/constants/index.ts'),
        enums: resolve(__dirname, 'src/enums/index.ts'),
        http: resolve(__dirname, 'src/http/index.ts'),
        interfaces: resolve(__dirname, 'src/interfaces/index.ts'),
        models: resolve(__dirname, 'src/models/index.ts'),
        network: resolve(__dirname, 'src/network/index.ts'),
        services: resolve(__dirname, 'src/services/index.ts'),
        storage: resolve(__dirname, 'src/storage/index.ts'),
        validators: resolve(__dirname, 'src/validators/index.ts'),
      },
      formats: ['es'],
      name: 'MyWebCore',
    },
    rollupOptions: {
      // make sure to externalize deps that shouldn't be bundled
      // into your library
      external: ['axios', 'date-fns', 'my-ts-core', 'js-base64', 'sprintf-js', 'vue'],
      output: {
        // Provide global variables to use in the UMD build
        // for externalized deps
        globals: {
          vue: 'Vue',
        },
        preserveModules: true, // Ensures file structure preserved in dist. Supports defineAsyncComponent.
        preserveModulesRoot: resolve(__dirname, 'src'), // Ensure correct module resolution. Supports defineAsyncComponent.
      },
    },
  },
  plugins: [vue()],
  server: {
    proxy: {
      '/family-expense/api': 'http://localhost:8088',
    },
  },
});
